var player;
var currLinkNumber;

function selectLink(link) {
    var currLink = document.getElementById("player-link-" + currLinkNumber.toString());
    if (currLink != null) {
        removeClass(currLink, 'bold');
        removeClass(currLink, 'colorPrimary-5');
        addClass(currLink, 'colorSecondary-a-2');
    }
    if (link != null) {
        addClass(link, 'bold');
        addClass(link, 'colorPrimary-5');
        removeClass(link, 'colorSecondary-a-2');
        currLinkNumber = parseInt(link.id.substr(link.id.lastIndexOf("-") + 1));
        var currItem = document.getElementById("player-item-" + currLinkNumber.toString());
        if (currItem != null) {
            player.pause();
            player.setSrc(currItem.value);
        }
    }
}

function playLink(link) {
    selectLink(link);
    player.play();
}

function playNextLink() {
    var nextLinkNumber = currLinkNumber + 1;
    var nextLink = document.getElementById("player-link-" + nextLinkNumber.toString());
    if (nextLink != null) {
        playLink(nextLink);
    }
}

$(document).ready(function () {
    player = new MediaElementPlayer('#player',
    {
        // width of audio player
        audioWidth: 900,
        // height of audio player
        audioHeight: 30,
        // initial volume when the player starts
        startVolume: 1.0,
        // the order of controls you want on the control bar (and other plugins below)
        features: ['playpause', 'progress', 'current', 'duration'],
        // forces the hour marker (##:00:00)
        alwaysShowHours: false,
        // turns keyboard support on and off for this instance
        enableKeyboard: true,
        // when this player starts, it will pause other players
        pauseOtherPlayers: true,
        // array of keyboard commands
        keyActions: []
    });

    player.media.addEventListener('ended', playNextLink, false);

    currLinkNumber = 0;
    var firstLink = document.getElementById("player-link-0");
    if (firstLink != null) {
        selectLink(firstLink);
    }
});
